App({
  onLaunch: function () {
    console.log('App Launch')
    var self = this;
    var rd_session = wx.getStorageSync('rd_session');
    console.log('rd_session', rd_session)
    if (!rd_session) {
      self.login();
    } else {
      wx.checkSession({
        success: function () {
          // 登录态未过期
          console.log('登录态未过期')
          self.rd_session = rd_session;
          self.getUserInfo();
        },
        fail: function () {
          //登录态过期
          self.login();
        }
      })
    }
  },
  onShow: function () {
    console.log('App Show')
  },
  onHide: function () {
    console.log('App Hide')
  },
  globalData: {
    hasLogin: false,
    // serverAddress:'http://localhost',
    serverAddress:'https://waimai.zhuantou.tv',
    shops: [
      {
        id: 1,
        img: 'http://wxapp.im20.com.cn/impublic/waimai/imgs/shops/shop_1.jpg',
        distance: 1.8,
        sales: 1475,
        logo: '/imgs/shop/ncepu.jpg',
        name: '华北电力大学餐厅',
        notice: '试运行期间学校部分区域使用',
        desc: '说明：仅支持校园卡支付'
      },
      {
        id: 2,
        img: 'http://wxapp.im20.com.cn/impublic/waimai/imgs/shops/shop_2.jpg',
        distance: 2.4,
        sales: 1284,
        logo: 'http://wxapp.im20.com.cn/impublic/waimai/imgs/shops/logo_2.jpg',
        name: '忠友麻辣烫(东四店)',
        desc: '满25减8；满35减10；满60减15（在线支付专享）'
      },
      {
        id: 3,
        img: 'http://wxapp.im20.com.cn/impublic/waimai/imgs/shops/shop_3.jpg',
        distance: 2.3,
        sales: 2039,
        logo: 'http://wxapp.im20.com.cn/impublic/waimai/imgs/shops/logo_3.jpg',
        name: '粥面故事(东大桥店)',
        desc: '满25减8；满35减10；满60减15（在线支付专享）'
      },
      {
        id: 4,
        img: 'http://wxapp.im20.com.cn/impublic/waimai/imgs/shops/shop_4.jpg',
        distance: 3.4,
        sales: 400,
        logo: 'http://wxapp.im20.com.cn/impublic/waimai/imgs/shops/logo_4.jpg',
        name: '兄鸡',
        desc: '满25减8；满35减10；满60减15（在线支付专享）'
      }
    ]
  },
  rd_session: null,
  login: function () {
    var self = this;
    wx.login({
      success: function (res) {
        console.log('wx.login', res)
        // server.getJSON('/WxAppApi/setUserSessionKey', {code: res.code}, function (res) {
        // 	console.log('setUserSessionKey', res)
        // 	self.rd_session = res.data.data.rd_session;
        self.globalData.hasLogin = true;
        // 	wx.setStorageSync('rd_session', self.rd_session);
        self.getUserInfo();
        // });
      }
    });
  },
  getUserInfo: function () {
    var self = this;
    wx.getUserInfo({
      success: function (res) {
        console.log('getUserInfo.success', res)
        self.globalData.userInfo = res.userInfo;
        var userInfo = res.userInfo
        var nickName = userInfo.nickName
        var avatarUrl = userInfo.avatarUrl
        var gender = userInfo.gender //性别 0：未知、1：男、2：女
        var province = userInfo.province
        var city = userInfo.city
        var country = userInfo.country
      },
      fail: function (res) {
        console.log('getUserInfo.fail', res);
        wx.showModal({
          showCancel: false,
          title: '获取用户信息失败',
          content: '请到右上角->关于->右上角->设置开启权限'
        });
      }
    });
  }
})
